package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.Message;
import com.sky.mapper.MessageMapper;
import com.sky.result.PageResult;
import com.sky.service.MessageService;
import com.sky.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public void submitMessage(MessageDTO messageDTO) {
        Message message = new Message();
        BeanUtils.copyProperties(messageDTO, message);
        message.setUserId(BaseContext.getCurrentId());
        message.setCreateTime(LocalDateTime.now());
        messageMapper.insert(message);
    }

    @Override
    public void replyMessage(ReplyDTO replyDTO) {
        Message message = new Message();
        message.setId(replyDTO.getId());
        message.setReply(replyDTO.getReply());
        message.setReplyTime(LocalDateTime.now());
        messageMapper.updateReply(message);
    }

    @Override
    public PageResult pageQuery(MessagePageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<MessageVO> page = messageMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public MessageVO getById(Long id) {
        return messageMapper.getById(id);
    }
}